define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'fastim/records/index' + location.search,
                    add_url: 'fastim/records/add',
                    edit_url: 'fastim/records/edit',
                    del_url: 'fastim/records/del',
                    multi_url: 'fastim/records/multi',
                    import_url: 'fastim/records/import',
                    message_url: 'fastim/records/index/session_id/{session_id}',
                    table: 'fastim_message',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"default":__('Type default'),"image":__('Type image'),"audio":__('Type audio'),"video":__('Type video'),"file":__('Type file'),"link":__('Type link'),"system":__('Type system'),"group_apply":__('Type group_apply'),"group_notice":__('Type group_notice'),"group_invitation":__('Type group_invitation'),"friend_apply":__('Type friend_apply'),"kbs_list":__('Type kbs_list'),"group_chat_notice":__('Type group_chat_notice'),"voice":__('Type voice')}, formatter: Table.api.formatter.normal},
                        {field: 'session_id', title: __('Session_id')},
                        {field: 'groupchat.nickname', title: __('Group'), operate: 'LIKE', placeholder: '群聊名称'},
                        {field: 'sender.nickname', title: __('Sender_id'), operate: false},
                        {field: 'recipient.nickname', title: __('Recipient_id'), operate: false},
                        {field: 'sender_id', title: __('Sender_id') + 'ID', visible: false, placeholder: 'IM用户ID'},
                        {field: 'recipient_id', title: __('Recipient_id') + 'ID', visible: false, placeholder: 'IM用户ID'},
                        {field: 'message', title: __('Message'), operate: 'LIKE', formatter: Controller.api.formatter.valueSubstring},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'record',
                                    title: '只看该会话',
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa fa-filter',
                                    url: $.fn.bootstrapTable.defaults.extend.message_url,
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                valueSubstring: function (value) {
                    let valueLength = value.replace(/[\u0391-\uFFE5]/g, "aa").length;
                    return valueLength > 30 ? value.substring(0, 30) + '...' : value;
                }
            }
        }
    };
    return Controller;
});